package com.molinari.coding.codici;

import java.util.HashMap;
import java.util.Iterator;

import com.molinari.androidstructure.activity.BaseActivity;
import com.molinari.coding.ControllerCoding;
import com.molinari.coding.R;
import com.molinari.coding.dati.domain.Cella;
import com.molinari.coding.dati.domain.dao.WrapCella;

public class Codici {

	public static final String[] keys = new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};

	public static final String[] indexs = new String[]{"1","2","3"};
	
	public HashMap<String, String[]> mapCodici;
	private BaseActivity activity;
	
	public String getValue(String key, String index){
		if(mapCodici.containsKey(key)){
			String[] codiciKey = mapCodici.get(key);
			int indiceInt = Integer.parseInt(index);
			indiceInt--;
			if(codiciKey != null && codiciKey.length > indiceInt){
				return codiciKey[indiceInt];
			}
		}
		return null;
	}
	
	public String[] getAllValue(String key1, String index1, String key2, String index2,String key3, String index3){
		String[] codiceAll = new String[3];
		codiceAll[0] = getValue(key1, index1);
		codiceAll[1] = getValue(key2, index2);
		codiceAll[2] = getValue(key3, index3);
		return codiceAll;
	}

	public HashMap<String, String[]> getMapCodici() {
		return mapCodici;
	}

	public void setMapCodici(HashMap<String, String[]> mapCodici) {
		this.mapCodici = mapCodici;
	}

	public static Codici getCodiciByConto(final BaseActivity activity) {
		Codici codici = new Codici();
		HashMap<String, String[]> mappaCodiciCreata = creaMappaByConto(activity);
		codici.setMapCodici(mappaCodiciCreata);
		
		return codici;
	}

	private static HashMap<String, String[]> creaMappaByConto(final BaseActivity activity) {
		
		final HashMap<String, String[]> mappaCodiciCreata = new HashMap<String, String[]>();
		
		final WrapCella wrapCella = new WrapCella(activity.getApplicationContext());
		int idUtente = ControllerCoding.getSingleton().getUtenteLogin().getidUtente();
		final Iterator<Object> all = (Iterator<Object>) wrapCella.selectByIdUtente(idUtente);
		
		if(all != null){
			while (all.hasNext()) {
				final Cella cella = (Cella) all.next();
				final String chiave = (String) cella.getChiave();
				final String[] split = new String[]{cella.getUno(),cella.getDue(), cella.getTre()};
				mappaCodiciCreata.put(chiave, split);
			}
		}
		return mappaCodiciCreata;
	}
}
